package com.bangbang.service;

import com.bangbang.pojo.Score;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
每个共同好友默认提供1分，但是在生活中如果次共同好友的好友数比较少，则说明次好友可能更加重要
**/
@Service
public class maybeService {
    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\friends.txt")));
        List<Score> scores = new ArrayList<Score>();
        String s = null;
        List<char[]> l = new ArrayList<>();
        Map<Character,Integer> map = new HashMap<>();
        while ((s=bufferedReader.readLine()) != null){
            char[] c = s.replace(" ","").toCharArray();
            l.add(c);
            map.put(c[0],c.length-1);
        }
        for (int i = 0;i<l.size();i++){
            for (int j = 0;j<l.size();j++){
                if (i != j){
                    Score score = aaa(l.get(i),l.get(j));
                    if(score != null){
                        List<Character> l1 = score.getL();
                        double wscore = 0.0;
                        for (char c:l1){
                            double sqrt = Math.sqrt(map.get(c));
                            double v = 1 / sqrt;
                            wscore += v;
                        }
                        double v = wscore /score.getUnion();
                        score.setWscore(v);
                        scores.add(score);
                    }
                }
            }
        }
        System.out.println(scores);

    }

    public static Score aaa(char[] a,char[] b){
        char x = a[0];
        for (char y:b){
            if (x==y){
                return null;
            }
        }
        List<Character> l = new ArrayList<>();

        for(int i=1;i<a.length;i++){
            for(int j=1;j<b.length;j++){
                if(a[i]==b[j]){
                    l.add(a[i]);
                }
            }
        }
        if(l.size()==0){
            return null;//没有共同好友返回null
        }
        Score score = new Score();
        score.setL(l);
        score.setA(a[0]);
        score.setB(b[0]);
        score.setIntersection(l.size());
        score.setUnion(a.length+b.length-l.size()-2);
        return score;


    }

}
